<!DOCTYPE html>
<html>
<head>
    <title>消息管理</title>
    <include file='static/head.html'></include>
    <style>
        [v-cloak] {
            display: none !important;
        }
    </style>
</head>
<body>

<!-- layui-tab开始 -->
<div class="layui-tab" id="app" v-cloak>

    <!-- 表格内容开始 -->
    <div class="layui-card">
        <div class="layui-card-body layui-nopadding " >
            <el-tabs type="border-card" v-loading="loading">
                <el-tab-pane label="邮件配置">
                    <el-form ref="email_form" :model="email_form" label-width="100px">
                        <el-form-item label="发送人姓名">
                            <el-input v-model="email_form.from_name"></el-input>
                        </el-form-item>
                        <el-form-item label="发送人邮箱">
                            <el-input v-model="email_form.from"></el-input>
                        </el-form-item>
                        <el-form-item label="邮件服务器">
                            <el-input v-model="email_form.host"></el-input>
                        </el-form-item>
                        <el-form-item label="发送协议">
                            <el-input v-model="email_form.smtp_secure"></el-input>
                        </el-form-item>
                        <el-form-item label="发送端口号">
                            <el-input v-model="email_form.port"></el-input>
                        </el-form-item>
                        <el-form-item label="验证用户名">
                            <el-input v-model="email_form.username"></el-input>
                        </el-form-item>
                        <el-form-item label="验证密码">
                            <el-input v-model="email_form.password"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmitEmail">提 交</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="短信配置">
                    <el-form ref="sms_form" :model="sms_form" label-width="80px">
                        <el-form-item label="KEY">
                            <el-input v-model="sms_form.key"></el-input>
                        </el-form-item>
                        <el-form-item label="密钥">
                            <el-input v-model="sms_form.secret"></el-input>
                        </el-form-item>

                        <el-row :gutter="5" v-for="(item, index) in sms_form.temp_arr">
                            <el-col :span="4">
                                <el-form-item label="模板标识" >
                                    <el-input v-model="item.temp_tag"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item label="模板名称" >
                                    <el-input v-model="item.temp_name"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item label="签名" >
                                    <el-input v-model="item.temp_autograph"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item label="模板号" >
                                    <el-input v-model="item.temp_code"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item label="备注" >
                                    <el-input v-model="item.temp_remark"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-button @click.prevent="addDomain(item)" v-if="(index+1) == sms_form.temp_arr.length" type="primary">添加</el-button>
                                <el-button @click.prevent="removeDomain(item)" v-if="index>0" type="danger">删除</el-button>
                            </el-col>
                        </el-row>

                        <el-form-item>
                            <el-button type="primary" @click="onSubmitSms">提 交</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="小程序配置">
                    <el-form ref="applet_form" :model="applet_form" label-width="100px">
                        <el-form-item label="小程序名称">
                            <el-input v-model="applet_form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="AppID">
                            <el-input v-model="applet_form.appid"></el-input>
                        </el-form-item>
                        <el-form-item label="AppSecret">
                            <el-input v-model="applet_form.appSecret"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmitApplet">提 交</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="公众号配置">
                    <el-form ref="official_form" :model="official_form" label-width="100px">
                        <el-form-item label="公众号名称">
                            <el-input v-model="official_form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="AppID">
                            <el-input v-model="official_form.appid"></el-input>
                        </el-form-item>
                        <el-form-item label="AppSecret">
                            <el-input v-model="official_form.appSecret"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmitOfficial">提 交</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="个推配置">
                    <el-form ref="getui_form" :model="getui_form" label-width="100px">
                        <el-form-item label="AppID">
                            <el-input v-model="getui_form.appid"></el-input>
                        </el-form-item>
                        <el-form-item label="AppKey">
                            <el-input v-model="getui_form.appkey"></el-input>
                        </el-form-item>
                        <el-form-item label="AppSecret">
                            <el-input v-model="getui_form.app_secret"></el-input>
                        </el-form-item>
                        <el-form-item label="MasterSecret">
                            <el-input v-model="getui_form.master_secret"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmitGetui">提 交</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
            </el-tabs>


        </div>
    </div>
    <!-- 表格内容结束 -->
</div>

<include file='static/scripts.html'></include>
<script type="text/javascript">
    let vm = new Vue({
        el: '#app',
        data: {
            loading:false,
            company_id:'',
            email_form: {
                from_name: '',//发送人姓名
                from: '',//发送人邮箱
                host: '',//邮件服务器
                smtp_secure: '',//发送协议
                port: '',//邮件发送端口号
                username: '',//验证用户名
                password: '',//验证密码
            },
            sms_form:{
                key:'',//key
                secret:'',//密钥
                temp_arr:[{
                    temp_tag:'',//模板标识
                    temp_name:'',//模板名称
                    temp_autograph:'',//签名
                    temp_code:'',//模板号
                    temp_remark:'',//模板备注
                }]//模板列表
            },
            sms_del_ids:[],//短信模板删除id
            applet_form:{
                name:'',//小程序名称
                appid:'',//APPID
                appSecret:'',//appSecret
            },
            official_form:{
                name:'',//公众号名称
                appid:'',//APPID
                appSecret:'',//appSecret
            },
            getui_form:{
                appid:'',
                appkey:'',
                app_secret:'',
                master_secret:'',
            }
        },
        //初始方法
        created: function () {
            this.company_id = '{$company_id}'
            this.getAllList()
        },
        methods: {
            //获取配置列表
            getAllList(){
                let that = this
                that.loading = true
                let url = "{:cmf_plugin_url('MessagePush://admin_index/getList')}";
                $.ajax({
                    url:url,
                    data:{
                        company_id:that.company_id
                    },
                    type:'post',
                    success:function(res){
                        if(res.code == 200){
                            if(res.data.email_form!=null){
                                that.email_form = res.data.email_form
                            }
                            if(res.data.sms_form!=null){
                                that.sms_form = res.data.sms_form
                            }
                            if(res.data.applet_form!=null){
                                that.applet_form = res.data.applet_form
                            }
                            if(res.data.official_form!=null){
                                that.official_form = res.data.official_form
                            }
                            if(res.data.getui_form!=null){
                                that.getui_form = res.data.getui_form
                            }
                        }
                        that.loading = false
                    },
                    error:function(){
                        that.$message.error('系统错误');
                        that.loading = false
                    }
                })
            },
            //邮件配置提交
            onSubmitEmail(){
                let that = this
                let url = "{:cmf_plugin_url('MessagePush://admin_index/editEmail')}";
                $.ajax({
                    url:url,
                    data:{
                        ...that.email_form,
                        company_id:that.company_id
                    },
                    type:'post',
                    success:function(res){
                        if(res.code == 200){
                            that.$message({
                                type: 'success',
                                message: res.message
                            });
                        }else{
                            that.$message({
                                type: 'error',
                                message: res.message
                            });                              
                        }
                    },
                    error:function(){
                        that.$message.error('提交失败请重试');
                    }
                })
            },
            ////////////////////////////////////////短信//////////////////////////////
            removeDomain(item) {
                let index = this.sms_form.temp_arr.indexOf(item)
                if (index !== -1) {
                    this.sms_form.temp_arr.splice(index, 1)
                }
                this.sms_del_ids.push(item.id)
            },
            addDomain() {
                this.sms_form.temp_arr.push({
                    temp_tag:'',//模板标识
                    temp_name:'',//模板名称
                    temp_autograph:'',//签名
                    temp_code:'',//模板号
                    temp_remark:'',//模板备注
                })
            },
            //短信配置提交
            onSubmitSms(){
                let that = this
                let url = "{:cmf_plugin_url('MessagePush://admin_index/editSms')}";
                $.ajax({
                    url:url,
                    data:{
                        ...that.sms_form,
                        sms_del_ids:that.sms_del_ids,
                        company_id:that.company_id
                    },
                    type:'post',
                    success:function(res){
                        if(res.code == 200){
                            that.$message({
                                type: 'success',
                                message: res.message
                            });
                        }else{
                            that.$message({
                                type: 'error',
                                message: res.message
                            });
                        }
                    },
                    error:function(){
                        that.$message.error('提交失败请重试');
                    }
                })
            },
            ////////////////////////////////////////小程序//////////////////////////////
            //小程序配置提交
            onSubmitApplet(){
                let that = this
                let url = "{:cmf_plugin_url('MessagePush://admin_index/editApplet')}";
                $.ajax({
                    url:url,
                    data:{
                        ...that.applet_form,
                        company_id:that.company_id
                    },
                    type:'post',
                    success:function(res){
                        if(res.code == 200){
                            that.$message({
                                type: 'success',
                                message: res.message
                            });
                        }else{
                            that.$message({
                                type: 'error',
                                message: res.message
                            });
                        }
                    },
                    error:function(){
                        that.$message.error('提交失败请重试');
                    }
                })
            },
            ////////////////////////////////////////公众号//////////////////////////////
            //公众号配置提交
            onSubmitOfficial(){
                let that = this
                let url = "{:cmf_plugin_url('MessagePush://admin_index/editOfficial')}";
                $.ajax({
                    url:url,
                    data:{
                        ...that.official_form,
                        company_id:that.company_id
                    },
                    type:'post',
                    success:function(res){
                        if(res.code == 200){
                            that.$message({
                                type: 'success',
                                message: res.message
                            });
                        }else{
                            that.$message({
                                type: 'error',
                                message: res.message
                            });
                        }
                    },
                    error:function(){
                        that.$message.error('提交失败请重试');
                    }
                })
            },
            ////////////////////////////////////////个推//////////////////////////////
            //个推配置提交
            onSubmitGetui(){
                let that = this
                let url = "{:cmf_plugin_url('MessagePush://admin_index/editGetui')}";
                $.ajax({
                    url:url,
                    data:{
                        ...that.getui_form,
                        company_id:that.company_id
                    },
                    type:'post',
                    success:function(res){
                        if(res.code == 200){
                            that.$message({
                                type: 'success',
                                message: res.message
                            });
                        }else{
                            that.$message({
                                type: 'error',
                                message: res.message
                            });
                        }
                    },
                    error:function(){
                        that.$message.error('提交失败请重试');
                    }
                })
            },
        }
    })
</script>
</body>
</html>